package com.woniu.controller;

import com.woniu.aspect.OpreLog;
import com.woniu.constant.Constant;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.entity.dto.ShopSearchDTO;
import com.woniu.pojo.Shop;
import com.woniu.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 门店控制器
 * @author tarnett
 */
@RestController
@RequestMapping("/shop")
@Slf4j
@Api(tags = "门店管理")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<Shop>> listAll(){
        List<Shop> shopList = shopService.listAll();
        return Result.ok(shopList);
    }

    @ApiOperation("搜索分页")
    @PostMapping("/search")
    public Result<PageResult<Shop>> listSearch(@RequestBody ShopSearchDTO shopSearchDTO){
        // 1. 对数据进行规则校验 TODO
        // 2. 调用业务逻辑层处理数据
        PageResult<Shop> pageResult = shopService.listSearch(shopSearchDTO);
        // 3. 接收数据，封装响应结果
        // 4. 返回数据
        return Result.ok(pageResult);
    }

    @OpreLog(title = "门店模块", businessType = Constant.BUSINESS_TYPE_SAVE)
    @ApiOperation("保存门店")
    @PostMapping
    public Result save(@RequestBody Shop shop){
        Result result = shopService.save(shop);
        return result;
    }

    @OpreLog(title = "门店模块", businessType = Constant.BUSINESS_TYPE_DELETE)
    @ApiOperation("批量删除")
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable Long[] ids){
        Integer row = shopService.deleteByIds(ids);
        return Result.commonByRow(row);
    }

    @OpreLog(title = "门店模块", businessType = Constant.BUSINESS_TYPE_UPDATE)
    @ApiOperation("修改门店")
    @PutMapping
    public Result update(@RequestBody Shop shop){
        Result result = shopService.updateById(shop);
        return result;
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public Result<Shop> getById(@PathVariable Long id){
        Shop shop = shopService.getById(id);
        return Result.ok(shop);
    }

    @ApiOperation("名称是否存在")
    @GetMapping("/check/name/{name}")
    public Result<Shop> checkNameExist(@PathVariable String name){
        Shop shop = shopService.checkNameExist(name);
        return Result.ok(shop);
    }
}
